<template>
  <div>
    <h5>设置超级管理员</h5>
    <Form :model="user" :rules="rules">
      <FormItem prop="username">
        <Input v-model="user.username" placeholder="请输入帐号">
          <Icon type="ios-contact" slot="prefix" />
        </Input>
      </FormItem>
      <FormItem prop="password">
        <Input type="password" v-model="user.password" placeholder="请输入密码">
          <Icon type="md-lock" slot="prefix" />
        </Input>
      </FormItem>
      <FormItem prop="email">
        <Input v-model="user.email">
          <Icon type="md-mail" slot="prefix" />
        </Input>
      </FormItem>
      <FormItem>
        <Button
          type="primary"
          large
          @click="setAdmin"
          :disabled="submitLoading"
          :loading="submitLoading"
        ></Button>
      </FormItem>
    </Form>
  </div>
</template>

<script>
import { mapActions } from "vuex";
import { emailVerify, getInitStatus } from "@/api/init";
import { register } from "@/api/profile";

export default {
  data() {
    return {
      user: {
        username: "",
        password: ""
      },
      timerText: "验证",
      rules: {
        username: [
          {
            required: true,
            message: "用户名不能为空"
          }
        ],
        password: [
          {
            required: true,
            message: "密码不能为空"
          }
        ],
        email: [
          {
            required: true,
            message: "请输入邮箱"
          },
          {
            type: "email",
            message: "请输入正确的邮箱"
          }
        ]
      },
      submitLoading: false,
      verifyDisabled: false
    };
  },
  methods: {
    setAdmin() {
      this.this.$refs.form.validate(res => {
        if (!res) {
          return;
        }
        this.submitLoading = true;
        register(this.user)
          .then(res => {
            this.submitLoading = false;
            await sendVerifyEmail();
            this.$Message.success("验证邮件发送成功，请到邮箱查收。")
            this.checkEmailStatus()
          })
          .catch(error => {
            this.submitLoading = false;
            this.remoteMessage = "用户名或密码错误";
          });
      });
    },
    // 清空用户表
    clearAdmin() {},

    checkEmailStatus() {

    },
    isComplete() {
        return getInitStatus().then(config=> config.value>=1) 
    }

  },
  async mounted() {
    if (await this.isComplete()) {
      return this.$emit("complete");
    }
    //
    this.clearAdmin();
  }
};
</script>

<style lang="less" scoped>
.login-wrap {
  display: flex;
  justify-content: center;
  flex-direction: column;
  margin: auto;
  height: 100vh;
  width: 300px;
}
.login {
  border-radius: 5px;
  box-shadow: 0 0 5px #ccc;
  padding: 14px;
}
.login-text {
  text-align: center;
  padding-bottom: 12px;
}
.login-bottom {
  display: flex;
  justify-content: space-between;
}
</style>
